﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Db;
using System.Threading;

namespace ManualCaChe
{

    /// <summary>
    /// 本工具只提供缓存移除，在程序中如果没有数据会自己尝试加载数据
    /// 所以这里值需要删除缓存中的数据
    /// </summary>
    public partial class Form1 : Form
    {
        //是否停止更新
        bool isStop = true;

        public Form1()
        {
            InitializeComponent();

            //允许跨线程
            Control.CheckForIllegalCrossThreadCalls = false;
        }


        #region 按钮操作
        
        /// <summary>
        /// 更新树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshTree();
        }
        
        /// <summary>
        /// 更新树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Refresh_Click(object sender, EventArgs e)
        {
            RefreshTree();
        }

        /// <summary>
        /// 全选或者取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ck_All_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.cbox_Table.Items.Count; i++)
            {
                this.cbox_Table.SetItemChecked(i, ck_All.Checked);
            }
            ck_All.Text = ck_All.Checked ? "取消" : "全选";
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_stop_Click(object sender, EventArgs e)
        {
            isStop = true;
        }

        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_ToCaChe_Click(object sender, EventArgs e)
        {
            but_ToCaChe.Enabled = false;

            if (cbox_Table.CheckedItems.Count != 0)
            {
                isStop = false;
                Thread td = new Thread(ToCaChe);
                td.Start();
            }
            else
            {
                MessageBox.Show("请选择一张表");
            }
        }


        #endregion

        /// <summary>
        /// 刷新树
        /// </summary>
        public void RefreshTree()
        {
            Db.DbTool tool = new Db.DbTool();
            var dic = tool.GetTableList();
            cbox_Table.Items.Clear();
            foreach (var key in dic.Keys)
            {
                cbox_Table.Items.Add(dic[key]);
            }
        }


        /// <summary>
        /// 获取需要更新的表
        /// </summary>
        public void ToCaChe()
        {
            try
            {
                Text_Log.Text = "";
                progress_Ing.Maximum = cbox_Table.CheckedItems.Count;
                progress_Ing.Value = 0;
                foreach (var box in cbox_Table.CheckedItems)
                {
                    if (isStop)
                    {
                        Text_Log.AppendText("\r\n 停止移除");
                        break;
                    }

                    Text_Log.AppendText("\r\n 正在移除:" + box.ToString());

                    yq.CaChe<object>.RefreshDelete(box.ToString().ExtToModelName());

                    progress_Ing.Value++;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新错误：" + ex.Message.ToString());
            }
            finally
            {
                isStop = true;
                but_ToCaChe.Enabled = true;
            }
        }

        


    }
}
